home *** CD-ROM | disk | FTP | other *** search
- Path: news.gate.net!pslfl2-45
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c
- Subject: Re: Question on pointer-to-pointer-to-pointer-...
- Date: 21 Jan 1996 03:51:03 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4dsd77$16qo@news.gate.net>
- References: <4dq2ej$m3t@cssun.cs.usm.my>
- NNTP-Posting-Host: pslfl2-45.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4dq2ej$m3t@cssun.cs.usm.my>,
- bahari@cs.usm.my (Bahari Belaton (Dr)) spake:
- ;Hi,
- ;Below is one of the question asked by my student about pointer-to-pointer in
- ;C - with a specific example on strtod() function. Can anyone help me explain
- ;this to my student. Especially on the important of pointer-to-pointer.
- ;
- ;Thanks
- ;
- ;Bahari Belaton
- ;
- ;=======================================================================
- ;Dear All,
- ; I hope you can enlighthen me a bit.
- ;Query 1:
- ; According to Deitel, the function prototype for strtod() is:-
- ; double strtod(const char *nPtr, char **endPtr) and the function call is
- ;as follows :-
- ; double d;
- ; char *string = "51.2% are admitted";
- ; char *stringPtr;
- ;
- ; d = strtod(string, &stringPtr);
- ;
- ;The book says that &stringPtr is assigned the location of the first character
- ;after the converted value. How does the function do that?
-
- If you have the varibles defined:
-
- char *stringPtr;
- char *string;
- double d;
-
- and call strtod:
-
- d = strtod(string, &stringPtr);
-
- The function might look something like this:
-
- double strtod(const char *s, char **ptr)
- {
- double retval;
-
- *ptr=s;
- while(/* **ptr fits strtod()'s form */) {
- /*process*/
- (*ptr)++;
- }
- return retval;
- }
-
- Now stringPtr, from the caller would hold a pointer to the value that stopped
- the scan.
-
- ;
- ;I'm still confused with **endPtr. Why the **endPtr(pointer to a
- ;pointer) is used?
-
- Because it is designed to return two values (char * and double). It could have
- returned a structure:
-
- struct strtod_struc {
- double retval;
- char * endptr;
- };
-
- To indirectly modify the pointer from the caller is a cleaner solution. You
- get your double, and a pointer to continue string processing with or
- determine what the hell is wrotten in Denmark (no offense to the Danish).
-
- ;(What is the significance of using a double pointer? Why
- ;not just *endPtr since the function assigned the address of first
- ;character of the string to &stringPtr.
-
- There would be no sense in passing it a char *, because it would not need it.
- What it needs is the address of your char * so it can modify it.
-
- ;(what about***endPtr? How many pointers to pointer can we used?)
-
- Never used that many levels in C and don't have the documentation. Someone
- else can enlighten us on that one.
-
- Bill
-
- "Whatcha got on?...Your mind?"
-